<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :page.sync="page" :data="data" ref="crud" v-model="form" :permission="permissionList" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange" @search-reset="searchReset" :before-open="beforeOpen" @selection-change="selectionChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuRight">
        <table-right :crud='$refs.crud' :data='data' :searchBtn="false" :columnBtn="true" :addPermission="permission.publicbasic_task_add" :handleDelete="handleDelete" :delPermission="permission.publicbasic_task_remove" :refreshChange='refreshChange'>
        </table-right>
      </template>
      <template slot-scope="{row}" slot="projectPhasesId">
        {{row.projectPhasesName}}
      </template>
      <template slot-scope="{scope,row}" slot="menuForm">
        <el-button icon="el-icon-circle-plus" v-if="type=='add'" v-loading="goon" size="mini" plain type="primary" @click.stop="saveAndGoon()">保存并继续</el-button>
      </template>
      <template slot-scope="{row}" slot="menu">
        <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
          <el-button icon="el-icon-edit" size="small" type="text" @click.stop="$refs.crud.rowEdit(row,row.$index)" v-if="permission.publicbasic_task_edit"></el-button>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
          <el-button icon="el-icon-delete" size="small" type="text" @click.stop="rowDel(row)" v-if="permission.publicbasic_task_remove"></el-button>
        </el-tooltip>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, add, remove, update, detail } from '@/api/project/productData/customer';
import { getQualityList } from '@/api/project/information/base';
import { getCustomerInfo } from '@/api/sale/contactManager/contactManager.js';
import { codeReg } from '@/util/zhengze';
import { mapGetters } from 'vuex';
import { init } from '@/mixins/init';
export default {
  mixins: [init],
  props: {
    activeName: {
      type: String,
    },
  },
  data() {
    return {
      option: {
        size: 'mini',
        dialogHeight: '100%',
        dialogWidth: '700',
        calcHeight: 80,
        tip: false,
        searchShow: true,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        menuWidth: 100,
        searchLabelWidth: 110,
        searchMenuSpan: 16,
        dialogClickModal: false,
        column: [
          {
            label: '客户编码',
            prop: 'customerNumber',
            minWidth: 150,
            span: 24,
            labelWidth: '110',
            disabled: true,
          },
          {
            label: '客户名称',
            prop: 'customerId',
            filterable: true,
            span: 24,
            labelWidth: '110',
            maxlength: 50,
            minWidth: 150,
            type: 'select',
            dicData: [],
            props: {
              label: 'customerName',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请选择客户名称',
                trigger: 'blur',
              },
            ],
            change: (value) => {
              this.customerList.map((item) => {
                if (item.id == value.value) {
                  this.form.customerNumber = item.customerNumber;
                }
              });
            },
          },
          {
            label: '客户特性等级',
            prop: 'customerLevel',
            span: 24,
            labelWidth: '110',
            maxlength: 10,
            minWidth: 350,
            rules: [
              {
                required: true,
                message: '请输入客户特性等级',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '本厂特性等级',
            prop: 'factoryLevel',
            span: 24,
            labelWidth: '110',
            maxlength: 50,
            minWidth: 150,
            type: 'select',
            dicData: [],
            props: {
              label: 'quaType',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请选择本厂特性等级',
                trigger: 'blur',
              },
            ],
          },
        ],
      },
      data: [],
      customerList: [],
    };
  },
  watch: {},
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.publicbasic_task_add, false),
        viewBtn: this.vaildData(this.permission.publicbasic_task_view, false),
        delBtn: this.vaildData(this.permission.publicbasic_task_remove, false),
        editBtn: this.vaildData(this.permission.publicbasic_task_edit, false),
      };
    },
  },
  mounted() {},
  created() {
    getQualityList().then((res) => {
      this.findObject(this.option.column, 'factoryLevel').dicData = res.data.data;
    });
    getCustomerInfo().then((res) => {
      this.findObject(this.option.column, 'customerId').dicData = res.data.data;
      this.customerList = res.data.data;
    });
  },
  methods: {
    rowSave(row, done, loading) {
      row.productId = this.productId;
      add(row).then(
        () => {
          if (this.goon) {
            loading();
            for (let key in this.$refs.crud.value) {
              this.$refs.crud.value[key] = null;
            }
            this.goon = false;
            this.onLoad(this.page);
          } else {
            this.onLoad(this.page);
            done();
          }
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        },
        (error) => {
          this.goon = false;
          window.console.log(error);
          loading();
        }
      );
    },
    // rowSave(row, done, loading) {
    //   add(row).then(
    //     () => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: 'success',
    //         message: '操作成功!',
    //       });
    //       done();
    //     },
    //     (error) => {
    //       window.console.log(error);
    //       loading();
    //     }
    //   );
    // },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      this.type = type;
      if (['edit', 'view'].includes(type)) {
        detail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style>
.box {
  height: 800px;
}

.el-scrollbar {
  height: 100%;
}

.box .el-scrollbar__wrap {
  overflow: scroll;
}
</style>
